﻿using Sundry.App_Start.Common;
using Sundry.BLL;
using Sundry.IBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Sundry.Areas.Admin.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Admin/Account/
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(Models.LoginModel model)
        {
            if (ModelState.IsValid)
            {
                string salt = "1234567890";
                //根据用户查找用户信息
                IAdminService _adminService = EntryService.GetServiceEntrace().GetAdminService;
                Sundry.Model.Admin adminModel = _adminService.LoadEntities(m => m.AdminUserName == model.UserName&&m.AuthoryId==1).FirstOrDefault();
                if (adminModel == null)
                {
                    ModelState.AddModelError("用户名不存在", "UserName");
                    return View(model);
                }
                else
                {
                    salt = adminModel.AdminSalt;
                    string password = adminModel.AdminPassword;
                    if (string.Equals(password, Sundry.Common.Md5Helper.CreatePassword(model.Password, salt)))
                    {
                        //设置管理员Cookie
                        Sundry.App_Start.Common.UserCookie.SetCookie(adminModel.AdminId.ToString(), adminModel.AdminUserName, adminModel.AdminNickName, adminModel.AdminLastLoginTime.ToString(), adminModel.AdminLastLoginIP, adminModel.AdminLastLoginAddress,adminModel.AuthoryId.ToString());
                        string loginIP = Sundry.App_Start.Common.IpSearch.GetRealIP();
                        string loginAddress = Sundry.App_Start.Common.IpSearch.GetAdrByIp();
                        //插入用户登录日志并修改用户登录信息
                        Sundry.Model.AdminLoginLog loginLogModel = new Model.AdminLoginLog()
                        {
                            AdminId = adminModel.AdminId,
                            AdminLoginAddress = loginAddress,
                            AdminLoginIP = loginIP,
                            AdminLoginTime = DateTime.Now,
                            AdminLoginBrowser = Sundry.App_Start.Common.IpSearch.GetBrowerVersion()
                        };
                        IAdminLoginLogService _adminLoginLogService = EntryService.GetServiceEntrace().GetAdminLoginLogService;
                        adminModel.AdminLastLoginIP = loginIP;
                        adminModel.AdminLastLoginTime = DateTime.Now;
                        adminModel.AdminLastLoginAddress = loginAddress;
                        int res = _adminLoginLogService.AddEntity(loginLogModel, adminModel);
                        if (res > 0)
                        {
                            return RedirectToAction("Index", "Home");
                        }
                        else
                        {
                            ModelState.AddModelError("密码错误", "Password");
                            return View(model);
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("登录失败", "ErroeMessage");
                        return View(model);
                    }
                }
            }
            return View(model);
        }


    }
}
